package com.wmh.baseservice.system.controller;

import com.wmh.baseservice.common.annotation.parameterverify.ParameterVerify;
import com.wmh.baseservice.common.ex.ServiceException;
import com.wmh.baseservice.common.utils.CommUtil;
import com.wmh.baseservice.common.utils.encrypt.Base64Util;
import com.wmh.baseservice.common.utils.page.pojo.PageInfo;
import com.wmh.baseservice.common.utils.page.pojo.PageResult;
import com.wmh.baseservice.common.utils.page.pojo.PageSortRoles;
import com.wmh.baseservice.common.utils.page.pojo.SortWay;
import com.wmh.baseservice.common.utils.page.util.PageUtil;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.system.pojo.SysBanner;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

import com.wmh.baseservice.system.service.SysBannerService;

/**
 * @author wmh generator
 * @since 2022-03-19
 */
@Api(tags = "系统轮播图表")
@RestController
@RequestMapping(value = "system/sysBanner", produces = "text/plain;charset=utf-8")
@RequiresRoles("admin")
public class SysBannerController {

    @Resource
    private SysBannerService sysBannerService;

    @ApiOperation("列表")
    @PostMapping("list")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "type", value = "0附近好地 1商城", required = true),
    })
    public String list(PageInfo pageInfo, Integer type) {
        pageInfo.setTimeScreen("create_time");
        pageInfo.setDefaultSort(SysBanner.SORT, SortWay.ASC);
        //执行分页辅助工具
        PageResult<SysBanner> pageResult = new PageUtil<SysBanner>(pageInfo).startPage((page, queryWrapper) -> {
            if (CommUtil.checkNull(type)) {
                queryWrapper.eq(SysBanner.TYPE, type);
            }
            sysBannerService.page(page, queryWrapper);
        });
        return ReturnBody.success(pageResult);
    }

    @ApiOperation("添加或修改")
    @PostMapping("addOrUpdate")
    @ParameterVerify(notNull = {"sysBanner.path", "sysBanner.type", "sysBanner.title", "sysBanner.sort"})
    public String addOrUpdate(SysBanner sysBanner) {
        if (sysBanner.getPath().contains("base64")) {
            String url = Base64Util.generateImage("banner/", sysBanner.getPath());
            if (!CommUtil.checkNull(url)) {
                return ReturnBody.error("图片上传错误");
            }
            sysBanner.setPath(url);
        }
        if (!sysBannerService.saveOrUpdate(sysBanner)) {
            throw new ServiceException();
        }
        return ReturnBody.success();
    }

    @ApiOperation("删除")
    @PostMapping("delete")
    @ApiImplicitParams({
             @ApiImplicitParam(paramType = "query", dataTypeClass = Long.class, name = "id", value = "id", required = true),
     })
    @ParameterVerify(notNull = "id")
    public String delete(Long id){
        if (!sysBannerService.removeById(id)) {
            throw new ServiceException();
        }
        return ReturnBody.success();
    }

}
